package puzzle;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Tester {
	
	public static void scanAndMove(String s,Tray tray){
		Scanner sc = new Scanner(s);
		int posx = sc.nextInt();
		int posy = sc.nextInt();
		Block b = tray.searchBlock(posx, posy);
		if (b==null)
			throw new IllegalStateException("There is no block here");
		int newx = sc.nextInt();
		int newy = sc.nextInt();
		
		if(newx!=posx){
			if(newy!=posy||(Math.abs(newx-posx))!=1)
				throw new IllegalStateException("You can't move the block here");
		}
		else{
			if((Math.abs(newy-posy))!=1)
				throw new IllegalStateException("You can't move the block here");
		}
		if(!tray.moveBlock(b, newx, newy))
			throw new IllegalStateException("You can't move the block here");
		else 
			System.out.println(b);
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {

			Tray myTray = new Tray();
			FileInputStream file1 = new FileInputStream(args[0]);
			FileInputStream file2 = new FileInputStream(args[1]);
			ReadFile read = new ReadFile(file1, file2,myTray);

			DrawElement.init(myTray);
			DrawElement.Draw(myTray);
			
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			String str = "";
			try {
				while ((str = in.readLine()) != null && str.length() != 0){
					try{
					scanAndMove(str,myTray);
					DrawElement.Draw(myTray);
					
					}catch(IllegalStateException e){
						e.printStackTrace();
					}
					if(myTray.win()){
						System.out.println("You win!!");
						System.exit(0);
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}
}
